const db = require('../tool/db.js');
const common = require('../model/common.js')
const message = require('../model/messageModel.js')

//添加优惠券
async function add(data){
    for (const i of data.gdata) {
        //获取商品信息
        let [[goodsinfo]] = await db.query('select * from goods where id = '+ i.gid)
        if(!goodsinfo){
            return {code:0,msg:'商品不存在',data:''}
        }
    } 

    let [insertRes] = await db.query('insert into `order` (number,price,uid,address,price2) values(?,?,?,?,?)',[data.number,data.price,data.uid,data.address,data.price2])
    if(insertRes.insertId){
        let [couponres] = await db.query('update user_coupon set status = 2 where couponid = ? and uid = ?',[data.couponid,data.uid])
        //获取刚才生成的订单id
        let [[orderinfo]] = await db.query('select id,number from `order` where number = ' + data.number)
        if(orderinfo.id){
            for (const i of data.gdata) {
                //获取商品信息
                let [[goodsinfo]] = await db.query('select * from goods where id = '+ i.gid)
                //保存商品快照
                let [res] = await db.query('insert into order_goods (oid,gid,gname,gprice,gcount,gphoto) values(?,?,?,?,?,?)',[orderinfo.id,goodsinfo.id,goodsinfo.name,goodsinfo.price,i.gcount,goodsinfo.photo]) 
            }
            return {code:1 ,msg:'支付成功',data:orderinfo}
        }
    }else{
        return {code:0 ,msg:'支付失败',data:''}
    }
}
//优惠券计算
async function coupon(data){
    let [couponExist] = await db.query('select * from user_coupon where couponid = ? and uid = ? and status = 1',[data.couponid,data.uid])
    if(!couponExist.length){
        return {code:0,msg:'优惠券不存在',data:data.price}
    }
    let [[couponinfo]] = await db.query('select * from coupon as b join ( select couponid,uid from user_coupon where couponid = ? and uid = ? and status = 1) as a on a.couponid = b.id',[data.couponid,data.uid])
    let date = Date.now()
    if(couponinfo.endTime < date){
        return {code:0,msg:'该优惠券已过期',data:data.price}
    }
    if(couponinfo.parentid == 1){
        if(data.price < couponinfo.condition){
            return {code:0,msg:'该优惠券不可用',data:data.price}
        }
        data.price = data.price - couponinfo.price
        if(data.price < 0){
            data.price = 0
        }
        return {code:1,msg:'成功',data:data.price} 
    }
    if(couponinfo.parentid == 2){
        data.price = data.price - couponinfo.price
        if(data.price < 0){
            data.price = 0
        }
        return {code:1,msg:'成功',data:data.price} 
    }
}
//管理员订单
async function adminList(data){
    let sql = ''
    //'select * from order_goods as c right join (select * from address as b right join ( select id as orderid,number,price2,address as addressnum,`status` from `order` where status = 1)as a on a.addressnum = b.id) as d on d.orderid = c.oid '
    switch (data.id) {
        case 1: //已发货
            sql = 'select gname,gprice,gcount,gphoto,uid,address,mobile,`name`,orderid,number,price2,`status`,`date` from order_goods as c right join (select * from address as b right join ( select id as orderid,number,price2,address as addressnum,`status`,`date` from `order` where status = 1)as a on a.addressnum = b.id) as d on d.orderid = c.oid'
            break;
        case 2: //未发货
            sql = 'select gname,gprice,gcount,gphoto,uid,address,mobile,`name`,orderid,number,price2,`status`,`date` from order_goods as c right join (select * from address as b right join ( select id as orderid,number,price2,address as addressnum,`status`,`date` from `order` where status = 0)as a on a.addressnum = b.id) as d on d.orderid = c.oid'
            break;
        case 3: //申请退款
            sql = 'select gname,gprice,gcount,gphoto,uid,address,mobile,`name`,orderid,number,price2,`status`,`date` from order_goods as c right join (select * from address as b right join ( select id as orderid,number,price2,address as addressnum,`status`,`date` from `order` where status = 2)as a on a.addressnum = b.id) as d on d.orderid = c.oid'
            break;
        case 4: //退款确定 已完成
            sql = 'select gname,gprice,gcount,gphoto,uid,address,mobile,`name`,orderid,number,price2,`status`,`date` from order_goods as c right join (select * from address as b right join ( select id as orderid,number,price2,address as addressnum,`status`,`date` from `order` where status = 3 || status = 4)as a on a.addressnum = b.id) as d on d.orderid = c.oid'
            break;
    
        default: sql = 'select gname,gprice,gcount,gphoto,uid,address,mobile,`name`,orderid,number,price2,`status`,`date` from order_goods as c right join (select * from address as b right join ( select id as orderid,number,price2,address as addressnum,`status`,`date` from `order`)as a on a.addressnum = b.id) as d on d.orderid = c.oid'
            break;
    }

    let [res] = await db.query(sql + ' order by orderid desc limit ? , ? ',[data.offset,data.num])
    if(res.length){
        return res
    }
}

async function adminMod(data){
    //1 发货
    //4 退款
    if(data.id === 1){
        let [res] = await db.query('update `order` set `status` = ?  where id = ?',[data.id,data.orderid])
        if(res.affectedRows){
            // let [[{status}]] = await db.query('select `status` from `order` where id = ' + data.orderid)
                message.adminSendSingle({ adminID:data.adminID,message:'您的订单已发货',uid:data.userid})
            return {code:1,msg:'操作成功',data:''}
        }
    }
    if(data.id == 4){
        let [[{price2}]] = await db.query('select price2 from `order` where id =  ' + data.orderid)
        let [res] = await db.query('update `order` set status = ? where id = ?',[data.id,data.orderid])
        if(res.affectedRows){
            
            message.adminSendSingle({ adminID:data.adminID,message:`您的订单已退款${price2}元`,uid:data.userid})
            return {code:1,msg:'退款成功',data:''}
        }
    }
    
}

async function userList(data){
    let sql = ''
    switch (data.id) {
        case 1: //待发货
            sql = 'select gname,gprice,gcount,gphoto,uid,address,mobile,`name`,orderid,number,price2,`status`,`date`  from order_goods as c right join (select * from address as b right join ( select id as orderid,number,price2,address as addressnum,`status`,`date` from `order` where uid = ? and status = 0)as a on a.addressnum = b.id) as d on d.orderid = c.oid'
            break;
        case 2: //待收货
            sql = 'select gname,gprice,gcount,gphoto,uid,address,mobile,`name`,orderid,number,price2,`status`,`date` from order_goods as c right join (select * from address as b right join ( select id as orderid,number,price2,address as addressnum,`status`,`date`  from `order` where uid = ? and status = 1)as a on a.addressnum = b.id) as d on d.orderid = c.oid'
            break;
        case 4: //待退款
            sql = 'select gname,gprice,gcount,gphoto,uid,address,mobile,`name`,orderid,number,price2,`status`,`date` from order_goods as c right join (select * from address as b right join ( select id as orderid,number,price2,address as addressnum,`status`,`date`  from `order` where uid = ? and status = 2)as a on a.addressnum = b.id) as d on d.orderid = c.oid'
            break;
        case 5: //完成
            sql = 'select gname,gprice,gcount,gphoto,uid,address,mobile,`name`,orderid,number,price2,`status`,`date` from order_goods as c right join (select * from address as b right join ( select id as orderid,number,price2,address as addressnum,`status`,`date`  from `order` where uid = ? and status = 3)as a on a.addressnum = b.id) as d on d.orderid = c.oid'
            break;
        default: sql = ' select gname,gprice,gcount,gphoto,uid,address,mobile,`name`,orderid,number,price2,`status`,`date` from order_goods as c right join (select * from address as b right join ( select id as orderid,number,price2,address as addressnum,`status`,`date`  from `order` where uid = ?)as a on a.addressnum = b.id) as d on d.orderid = c.oid'
            break;     
    }

    let [res] = await db.query(sql,[data.uid])
    if(res.length){
        return res
    }
}

async function userMod(data){

    //退款 2
    //收货 3
    if(data.id == 3){
        let [res] = await db.query('update `order` set status = 3 where id = ?',[data.id,data.orderid])
        if(res.affectedRows){
            return {code:1,msg:'成功',data:''}
        }
    }
    let [res] = await db.query('update `order` set status = ? - status where id = ?',[data.id,data.orderid])
    if(res.affectedRows){
        return {code:1,msg:'已发起退款,请耐心等待工作人员处理',data:''}
    }
}


module.exports = { add,coupon,adminList,adminMod,userList,userMod}